package scm.inventoryControl.managedbean;

import commonInfrastructure.accountManagement.entity.WarehousePersonnelEntity;
import commonInfrastructure.accountManagement.session.WarehousePersonnelSessionBean;
import java.io.IOException;
import scm.inventoryControl.entity.ProductInventoryLevelEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class ViewFinalGoodsInventoryManagedBean implements Serializable {

    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    
    private ProductInventoryLevelEntity selectedInventory;
    private List<String> test;
    private boolean canEdit;
     
    @EJB
    private WarehousePersonnelSessionBean warehouseSession;
    
    public ViewFinalGoodsInventoryManagedBean() {
        test=new ArrayList();
        warehouseSession = new WarehousePersonnelSessionBean();
    }
    
    public void initView(PhaseEvent event){
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        WarehousePersonnelEntity user = warehouseSession.getWarehousemanById(loginId);
        if (user.getWarehouseLevel().equals("Warehouse Manager")){
            setCanEdit(true);
        }
        else{
            setCanEdit(false);
        }
    }
    
    public void gotoEdit(ActionEvent e) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("thisInventory", getProductInventories());
        FacesContext.getCurrentInstance().getExternalContext().redirect("editFinalGoodInventoryLevel.xhtml");
    }
    
    public List<String> getLocationList(){
        if(selectedInventory.getProduct().getProductName()=="Classic Nutri Bar-Nuts")
        test.add("Sector A");
        else if(selectedInventory.getProduct().getProductName()=="Classic Nutri Bar-Fruits")
            test.add("Sector B");
        return test;
    }

    public List<ProductInventoryLevelEntity> getProductInventories() {
        return inventorySessionBean.getAllProductInventories();
    }

//      public void viewProcessing(ActionEvent event){
//           setSelectedInventory((ProductInventoryLevelEntity)event.getComponent().getAttributes().get("ViewRecord"));
//           System.out.println("@@@@@@@@@@@@@@@@@@@@@@@"+selectedInventory.getProductBatchRecords().get(0).getQuantity());
//           test = selectedInventory.getProductBatchRecords().get(0).getQuantity();
//           System.out.println("&&&&&&&&&&&&&&&"+test);
//      }
      
    public ProductInventoryLevelEntity getSelectedInventory() {
        return selectedInventory;
    }

    public void setSelectedInventory(ProductInventoryLevelEntity selectedInventory) {
        this.selectedInventory = selectedInventory;
    }

    public List<String> getTest() {
        return test;
    }

    public void setTest(List<String> test) {
        this.test = test;
    }

    /**
     * @return the canEdit
     */
    public boolean isCanEdit() {
        return canEdit;
    }

    /**
     * @param canEdit the canEdit to set
     */
    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

 

}
